//
//  PhotoDetailsController.m
//  Streaming
//
//  Created by 官琛 on 15-6-24.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "PhotoDetailsController.h"

@interface PhotoDetailsController ()

@end

@implementation PhotoDetailsController

- (void)dealloc
{
    [self.array release];
    [self.scrollView release];
    [self.smallView release];
    [self.imageArray release];
    [self.indexImage release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
   
    self.scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(WIDTH*(self.array.count+2), 0);
    self.scrollView.pagingEnabled = YES;//按页滑动
    self.scrollView.bounces = NO;//边界反弹
    self.scrollView.showsHorizontalScrollIndicator = NO;//滑动条
    [self.view addSubview:self.scrollView];
    [self.scrollView release];
    
    self.imageArray = [NSMutableArray array ];
    
    int i = 0;
    for (Photo *pho in self.array) {
        i = i+1;
        self.smallView = [[UIScrollView alloc]initWithFrame:CGRectMake(WIDTH*(i), 0, WIDTH, HEIGHT)];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
            NSURL *url = [NSURL URLWithString:pho.image];
            [image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"12346.jpg"]];
//        NSLog(@"  i = %d",i);
        image.contentMode = UIViewContentModeScaleAspectFit;
        [self.smallView addSubview:image];
        [self.scrollView addSubview:self.smallView];
        self.smallView.minimumZoomScale = 0.5;
        self.smallView.maximumZoomScale = 2.0;
        self.smallView.delegate = self;
        [self.imageArray addObject:image];
        [self.smallView release];
        [image release];
        }
//    NSLog(@"arr=%ld img= %ld",self.array.count,self.imageArray.count);
   
    
    
    UIImageView *imageHead = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    imageHead = [self.imageArray lastObject];
    imageHead.contentMode = UIViewContentModeScaleAspectFit;
    [self.scrollView addSubview:imageHead];
    
    UIImageView *imageFoot = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH*(self.array.count+1), 0, WIDTH, HEIGHT)];
    imageFoot.contentMode = UIViewContentModeScaleAspectFit;
    imageFoot = [self.imageArray firstObject];
    [self.scrollView addSubview:imageFoot];
  
    UIImageView *imageDetails = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    imageDetails.contentMode = UIViewContentModeScaleAspectFit;
    imageDetails.autoresizesSubviews = YES;
    imageDetails.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    NSString *str = [NSString stringWithFormat:@"%@",self.indexImage.image];
    NSURL *urlStr = [NSURL URLWithString:str];
    [imageDetails sd_setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"12346.jpg"]];
    self.scrollView.contentOffset = CGPointMake(WIDTH*(self.row+1), 0);
    [self.scrollView addSubview:imageDetails];
    
    

//    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
    
    [imageDetails release];
    
    [imageHead release];
    [imageFoot release];
    
   


}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{//中心放大缩小
    for (int i = 0; i < self.array.count+2 ; i++) {
        UIImageView *tempimage = self.imageArray[i];
        tempimage.center = self.view.center;
    }
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews objectAtIndex:0];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{//减速结束时
    //放大缩小还原
    for (UIScrollView *tempScroll in scrollView.subviews) {
        if ([tempScroll isKindOfClass:[UIScrollView class]]) {
            tempScroll.zoomScale = 1.0;
        }
    }
    //循环
    if (self.scrollView.contentOffset.x > WIDTH*self.array.count + 100) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    if (self.scrollView.contentOffset.x < 200) {
        self.scrollView.contentOffset = CGPointMake(WIDTH*self.array.count , 0);
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
